#include <REGX52.H>
#include "LCD1602.h"
#include "MatrixKey.h"

unsigned char keyNumber = 0;
unsigned int password = 0;
unsigned char count = 0;

void main()
{
	LCD_Init();
	LCD_ShowString(1, 1, "Password:");

	while(1)
	{
		keyNumber = getMatrixKey();
		if(keyNumber)
		{
			// 若按键是1-10，则设置为密码
			if(keyNumber <= 10)
			{
				// 如果按键次数小于4
				if(count < 4)
				{
					password *= 10; // 密码左移一位
					password += keyNumber % 10; // 获取一位密码
					count++;
				}
				LCD_ShowNum(2, 1, password, 4); // 更新显示
			}
			// 若按键是11，则进行确认操作
			if(keyNumber == 11)
			{
				if(password == 2345)
				{
						LCD_ShowString(1, 14, "OK ");
				}
				else
				{
						LCD_ShowString(1, 14, "ERR");
				}
				password = 0;
				count = 0;
				LCD_ShowNum(2, 1, password, 4);
			}
			// 若按键是12，则进行取消操作
			if(keyNumber == 12)
			{
				password = 0;
				count = 0;
				LCD_ShowNum(2, 1, password, 4);
			}
		}

	}
}